﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace TowerDefence_Zombie
{
    public class EntityTowerPreview : EntityLogicEx
    {
        [SerializeField] private float sphereCastRadius = 1;
        [SerializeField] private float dampSpeed = 5;
        [SerializeField] private Material material;
        [SerializeField] private Material invalidPositionMaterial;

        private EntityDataTowerPreview entityDataTowerPreview;
        private bool visible = true;
        private Renderer[] renderers;

        public bool IsVisualizerComplete { get; private set; }

        public EntityRadiusVisualizer RadiusVisualiser { get; private set; }

        protected override void OnInit(object userData)
        {
            base.OnInit(userData);
            renderers = transform.GetComponentsInChildren<MeshRenderer>(true);
        }

        protected override void OnShow(object userData)
        {
            base.OnShow(userData);

            entityDataTowerPreview = userData as EntityDataTowerPreview;
            if (entityDataTowerPreview == null)
            {
                Log.Error("EntityDataTowerPreview param is invaild");
                return;
            }

            SetVisiable(true);
        }

        protected override void OnHide(bool isShutdown, object userData)
        {
            base.OnHide(isShutdown, userData);
            entityDataTowerPreview = null;
        }

        private void SetVisiable(bool value)
        {
            if (visible == value)
                return;

            foreach (var item in renderers)
            {
                item.enabled = value;
            }
            visible = value;
        }

        public void OnVisualizerCreat(EntityRadiusVisualizer radiusVisualiser)
        {
            IsVisualizerComplete = true;
            RadiusVisualiser = radiusVisualiser;

        }
    }
}

